/*
 * Created by JFormDesigner on Fri Jul 01 09:51:27 CST 2022
 */

package View.TransSelectPicture;

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.*;

import Factory.CalculateClassFactory;
import Factory.ENUM.CAL_MODE;
import Model.DrawModel.CalPoint;
import Model.DrawModel.HistoricalTrack;
import Model.SurroundModel.Ship;
import Model.TransFormModel.Sampling;
import Model.TransFormModel.TMat;
import Service.ShipCalculate;
import View.PublicWindow.OkAttention;
import View.PublicWindow.WrongAttention;
import com.jgoodies.forms.factories.*;

/**
 * @author Brainrain
 */
public class SelectPicturePanel extends JPanel {
    public SelectPicturePanel() {
        initComponents();
    }

    //文件选择器
    private static JFileChooser chooser = new JFileChooser();//java提供的文件选择器;

    //采样器
    static Sampling samp1 = new Sampling(100);
    static Sampling samp2 = new Sampling(400);

    // 图片绝对路径
    private String PictureOnePath = "";
    private String PictureTwoPath = "";
    private String PictureOneChangePath = "";
    private String PictureTwoChangePath = "";

    static ArrayList<Ship> FromShips = null;  //初始阵型
    static ArrayList<Ship> EndShips = null;   //结束阵型

    static TMat tMat;

    // 船数
    private int ShipsNumber = 70;

    //输入处理器
    public static String input() {

        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

        int returnVal = chooser.showOpenDialog(chooser);

        //如果找到文件则保存地址
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            return chooser.getSelectedFile().getAbsolutePath();
        } else
            return null;
    }

    private void button1MouseClicked(MouseEvent e) {
        // TODO add your code here
        System.out.println("点击选择按钮");

        // 对话框
        String file = input();
        if (file != null) {
            PictureOnePath = file;
            ImageIcon image = new ImageIcon(PictureOnePath);
            image.setImage(image.getImage().getScaledInstance(250,195,Image.SCALE_DEFAULT));
            label1.setIcon(image);
        } else {
            PictureOnePath = "";
            label1.setIcon(null);
        }
    }

    private void button2MouseClicked(MouseEvent e) {
        // TODO add your code here

        // 对话框
        String file = input();
        if (file != null) {
            PictureTwoPath = file;
            ImageIcon image = new ImageIcon(PictureTwoPath);
            image.setImage(image.getImage().getScaledInstance(250,195,Image.SCALE_DEFAULT));
            label2.setIcon(image);
        }else {
            PictureTwoPath = "";
            label2.setIcon(null);
        }

    }

    private void button4MouseClicked(MouseEvent e) {
        // TODO add your code here
        System.out.println("点击效果模拟");
        if (!"".equals(PictureTwoPath) && !"".equals(PictureOnePath)) {
            try {
                String newFile1 = PictureOnePath.substring(0, PictureOnePath.lastIndexOf("\\")) + "\\PictureOne提取后图片.jpg";
                String newFile2 = PictureTwoPath.substring(0, PictureTwoPath.lastIndexOf("\\")) + "\\PictureTwo提取后图片.jpg";

                samp1.transfer(PictureOnePath, newFile1);
                PictureOneChangePath = newFile1;
                ImageIcon image1 = new ImageIcon(PictureOneChangePath);
                image1.setImage(image1.getImage().getScaledInstance(250,195,Image.SCALE_DEFAULT));
                label1.setIcon(image1);

                samp2.transfer(PictureTwoPath, newFile2);
                PictureTwoChangePath = newFile2;
                ImageIcon image2 = new ImageIcon(PictureTwoChangePath);
                image2.setImage(image2.getImage().getScaledInstance(250,195,Image.SCALE_DEFAULT));
                label2.setIcon(image2);

                // 根据目标数量整理船队数量
                samp1.alterAmount(PictureOnePath, PictureOneChangePath, ShipsNumber);
                samp1.ResetId();

                samp2.alterAmount(PictureTwoPath, PictureTwoChangePath, ShipsNumber);
                samp2.ResetId();

                FromShips = samp1.appointShip;
                EndShips = samp2.appointShip;
            } catch (IOException ioException) {
                new WrongAttention().setVisible(true);
            }

            System.out.println("操作完成！");
            new OkAttention().setVisible(true);

        } else {

            System.out.println("异常操作！");
            new WrongAttention().setVisible(true);

        }
    }

    public ShipCalculate GetService(
            ArrayList<CalPoint> targetList,
            ArrayList<CalPoint> shipsList,
            ArrayList<CalPoint> VirtualTargetsList,
            CalPoint center,
            ArrayList<HistoricalTrack> historicalShipsList,
            int sleepTime
    ) {
        if (FromShips == null || EndShips == null) {
            System.out.println("船队为0，无法提取");
            return null;
        } else {
            System.out.println("提取成功");
            return CalculateClassFactory.getService(targetList, shipsList,VirtualTargetsList,center, historicalShipsList,sleepTime, CAL_MODE.TRANS, FromShips, EndShips);
        }
    }

    private void textField1CaretUpdate(CaretEvent e) {
        // TODO add your code here
        ShipsNumber = Integer.parseInt(textField1.getText());
        System.out.println(ShipsNumber);
    }

    private void textField1PropertyChange(PropertyChangeEvent e) {
        // TODO add your code here
    }

    private void textField1InputMethodTextChanged(InputMethodEvent e) {
        // TODO add your code here
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        DefaultComponentFactory compFactory = DefaultComponentFactory.getInstance();
        button1 = new JButton();
        button2 = new JButton();
        label1 = new JLabel();
        label2 = new JLabel();
        textField1 = new JTextField();
        title1 = compFactory.createTitle("\u8230\u8239\u6570\u91cf\uff1a");
        button4 = new JButton();

        //======== this ========
        setLayout(null);

        //---- button1 ----
        button1.setText("\u9009\u62e9");
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button1MouseClicked(e);
            }
        });
        add(button1);
        button1.setBounds(new Rectangle(new java.awt.Point(100, 35), button1.getPreferredSize()));

        //---- button2 ----
        button2.setText("\u9009\u62e9");
        button2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button2MouseClicked(e);
            }
        });
        add(button2);
        button2.setBounds(390, 35, 78, 30);

        //---- label1 ----
        label1.setText("\u521d\u59cb\u56fe\u7247");
        add(label1);
        label1.setBounds(20, 75, 250, 195);

        //---- label2 ----
        label2.setText("\u76ee\u6807\u56fe\u7247");
        add(label2);
        label2.setBounds(305, 75, 250, 195);

        //---- textField1 ----
        textField1.setText("70");
        textField1.addPropertyChangeListener(e -> textField1PropertyChange(e));
        add(textField1);
        textField1.setBounds(305, 335, 70, 40);
        add(title1);
        title1.setBounds(215, 340, title1.getPreferredSize().width, 30);

        //---- button4 ----
        button4.setText("\u6570\u636e\u63d0\u53d6");
        button4.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button4MouseClicked(e);
            }
        });
        add(button4);
        button4.setBounds(250, 450, 82, 30);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < getComponentCount(); i++) {
                Rectangle bounds = getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            setMinimumSize(preferredSize);
            setPreferredSize(preferredSize);
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JButton button1;
    private JButton button2;
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JLabel title1;
    private JButton button4;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
